//
//  EnrollTextFieldView.m
//  练习_12.01_
//
//  Created by rimi1 on 2016/12/1.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "EnrollTextFieldView.h"

static NSInteger titleHeight_ = 14;

@interface EnrollTextFieldView () <UITextFieldDelegate>

@property (nonatomic, copy) NSString *titleText;
@property (nonatomic, copy) NSString *placeholder;
@property (nonatomic, copy) NSString *errorText;

@end

@implementation EnrollTextFieldView

- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title placeholder:(NSString *)placeholder error:(NSString *)error {
    frame.size.height = WXQAdaptionHeight(titleHeight_) + 44;
    self = [super initWithFrame:frame];
    if (self) {
        self.titleText = title;
        self.placeholder = placeholder;
        self.errorText = error;
        [self addSubview:self.title];
        [self addSubview:self.textField];
    }
    return self;
}
// 真则对
- (void)showImageWithTure:(BOOL)ture {
    if (ture) {
        self.tureOrFalse.image = [UIImage imageNamed:@"对"];
        self.textField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    }else {
        self.tureOrFalse.image = [UIImage imageNamed:@"错"];
        self.textField.layer.borderColor = [UIColor redColor].CGColor;
    }
    [self addSubview:self.tureOrFalse];
}
// 真则删除
- (void)showErrorWithTure:(BOOL)ture {
    if ([self.errorText isEqualToString:@""] || !self.errorText) {
        
    }else {
        if (ture) {
            [self.error removeFromSuperview];
        }else {
            [self addSubview:self.error];
        }
    }
    
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    if (self.endEdit) {
        self.endEdit();
    }
    
    if (textField.text.length == 0) {
        [self.tureOrFalse removeFromSuperview];
        [self.error removeFromSuperview];
        self.textField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    }
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *str = textField.text;
    if (str.length >= self.maxLength && string.length != 0) {
        return NO;
    }else {
        return YES;
    }
}

#pragma mark - get

- (UILabel *)title {
    
    if (!_title) {
        
        CGFloat height = WXQAdaptionHeight(titleHeight_);
        CGFloat width = [self.titleText boundingRectWithSize:CGSizeMake(self.bounds.size.width, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:WXQAdaptionHeight(titleHeight_)]} context:nil].size.width;
        
        _title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, height)];
        _title.font = [UIFont systemFontOfSize:WXQAdaptionHeight(titleHeight_)];
        _title.text = self.titleText;
        
    }
    return _title;
}

- (UILabel *)error {
    
    if (!_error) {
        
        _error = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.title.frame), CGRectGetMinY(self.title.frame), self.bounds.size.width - CGRectGetWidth(self.title.bounds), CGRectGetHeight(self.title.bounds))];
        _error.font = self.title.font;
        _error.text = [NSString stringWithFormat:@"(%@)", self.errorText];
        _error.textColor = [UIColor redColor];
        
    }
    return _error;
}

- (UITextField *)textField {
    
    if (!_textField) {
        
        _textField = [[UITextField alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.title.frame) + 5, self.bounds.size.width - CGRectGetWidth(self.tureOrFalse.bounds) - WXQAdaptionWidth(10), 44)];
        _textField.placeholder = self.placeholder;
        _textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];
        _textField.leftViewMode = UITextFieldViewModeAlways;
        _textField.layer.cornerRadius = 3;
        _textField.layer.borderWidth = 1;
        _textField.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _textField.delegate = self;
        _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        
    }
    return _textField;
}

- (UIImageView *)tureOrFalse {
    
    if (!_tureOrFalse) {
        
        CGFloat width = 20;
        _tureOrFalse = [[UIImageView alloc] initWithFrame:CGRectMake(self.bounds.size.width - width, 0, width, width)];
        CGPoint point = _tureOrFalse.center;
        point.y = self.textField.center.y;
        _tureOrFalse.center = point;
        
    }
    return _tureOrFalse;
}


@end












